package org.esti.service.impl;

import org.apache.commons.io.FileUtils;
import org.esti.model.User;
import org.esti.model.dao.UserDAO;
import org.esti.model.exceptions.UserAlreadyExistException;
import org.esti.service.UserService;
import org.esti.service.exception.NotValidImageContentTypeException;
import org.esti.service.exception.SavingAvatarException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.util.Date;
import java.util.List;


/**
 * @author Artem Loginov (logart)
 */

/**
 * {@inheritDoc}
 */
@Service
public class UserServiceImpl implements UserService {

    @Autowired
    private UserDAO dao;

    @Value("${storage.avatarPath}")
    private String avatarPath;

    /**
     * {@inheritDoc}
     */
    @Override
    public User createUser(User user) {
        if (!dao.isUserExist(user)) {
            return dao.add(user);
        } else {
            throw new UserAlreadyExistException("user with login " + user.getLogin() + " already exist");
        }
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public boolean isUserExist(User user) {
        return dao.isUserExist(user);
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public User getUserByLogin(String login) {
        return dao.getByLogin(login);
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public User updateUser(
            String login,
            String name,
            String middleName,
            String surname,
            Date birthday) {

        User user = getUserByLogin(login);

        user.setName(name);
        user.setMiddleName(middleName);
        user.setSurname(surname);
        user.setBirthday(birthday);

        return dao.update(user);

    }

    @Override
    public User updateUser(User user) {
        return dao.update(user);
    }

    private String getImageExtensionByContentType(String contentType) {
        if (contentType.contains("image")) {
            return contentType.substring(contentType.indexOf('/') + 1);
        } else {
            throw new NotValidImageContentTypeException();
        }

    }


    @Override
    public void updatePhoto(String login, MultipartFile image) {

        String avatarLink = "avatars/" + login +
                "." +
                getImageExtensionByContentType(image.getContentType());

        try {

            File file = new File(
                    avatarPath + avatarLink);


            FileUtils.writeByteArrayToFile(file, image.getBytes());


        } catch (IOException e) {
            throw new SavingAvatarException(e);
        }
        User user = dao.getByLogin(login);
        user.setAvatar("/resources/" + avatarLink);
        dao.update(user);


    }

    @Override
    public List<User> getUsers() {
        return dao.getAllUsers();
    }


    @Override
    public List<User> getUsers(String keyword) {
        return dao.getUsersByKeyWord(keyword);
    }
}
